package cn.stylefeng.guns.order.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.order.dto.OrderDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import cn.stylefeng.guns.order.service.OrderService;
import cn.stylefeng.guns.order.model.param.OrderParam;

import javax.annotation.Resource;

/**
 * order 控制器
 *
 * @author: aria
 * @date: 2021/02/20 20:24
 */
@Api(tags = "订单相关")
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 分页查询order
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("分页查询")
    @GetMapping("/order/page")
    @BusinessLog(title = "order_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(OrderParam orderParam) {
        OrderDTO orderDTO = new OrderDTO();
        BeanUtil.copyProperties(orderParam,orderDTO);
        return new SuccessResponseData(orderService.page(orderDTO));
    }

    /**
     * 获取全部order
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("获取所有订单")
    @GetMapping("/order/list")
    @BusinessLog(title = "order_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(OrderParam orderParam) {
        OrderDTO orderDTO = new OrderDTO();
        BeanUtil.copyProperties(orderParam,orderDTO);
        return new SuccessResponseData(orderService.list(orderDTO));
    }
    /**
     * 根据用户id获取该用户的所有订单
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("根据用户id查询全部订单记录,只需要传userId")
    @GetMapping("/order/listByUserId")
    @BusinessLog(title = "order_查询某用户的所有订单", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData listByUserId(OrderParam orderParam) {
        OrderDTO orderDTO = new OrderDTO();
        BeanUtil.copyProperties(orderParam,orderDTO);
        return new SuccessResponseData(orderService.listByUserId(orderDTO));
    }

    /**
     * 查看详情order
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("根据订单id获取订单详情,只需要传id")
    @GetMapping("/order/detail")
    @BusinessLog(title = "order_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail( OrderParam orderParam) {
        OrderDTO orderDTO = new OrderDTO();
        BeanUtil.copyProperties(orderParam,orderDTO);
        return new SuccessResponseData(orderService.detail(orderDTO));
    }
    @ApiOperation("付款(只需要传order_id)")
    @GetMapping("/order/pay")
    @BusinessLog(title = "付款", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData pay( OrderParam orderParam) {
        OrderDTO orderDTO = new OrderDTO();
        BeanUtil.copyProperties(orderParam,orderDTO);
        return orderService.pay(orderDTO);
    }

    /**
     * 添加order
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("添加订单")
    @PostMapping("/order/add")
    @BusinessLog(title = "order_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(OrderParam.add.class) OrderParam orderParam) {
        ResponseData add = null;
        try {
            OrderDTO orderDTO = new OrderDTO();
            BeanUtil.copyProperties(orderParam,orderDTO);
            add = orderService.add(orderDTO);
            return add;
        } catch (RuntimeException e) {
            return new ErrorResponseData(500, "出现意外错误,请联系管理员");
        }

    }

    /**
     * 删除order
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("根据订单id删除订单,只需要传id")
    @PostMapping("/order/delete")
    @BusinessLog(title = "order_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody  OrderParam orderParam) {
        OrderDTO orderDTO = new OrderDTO();
        BeanUtil.copyProperties(orderParam,orderDTO);
        orderService.delete(orderDTO);
        return new SuccessResponseData();
    }

    /**
     * 编辑order
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @PostMapping("/order/edit")
    @BusinessLog(title = "order_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody  OrderParam orderParam) {
        OrderDTO orderDTO = new OrderDTO();
        BeanUtil.copyProperties(orderParam,orderDTO);
        orderService.edit(orderDTO);
        return new SuccessResponseData();
    }


}
